#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json

'''
3. dump: 将Python对象转换成JSON串, 输出到fp文件流中, fp是一个支持write()的类文件对象
4. dumps:把Python对象转换成JSON串
'''

py_obj = {'employees': [{'first name': True, 'last name': None}, {'first name': 'Bill', 'last name': 'Gates'},
                        {'first name': 'zhang', 'last name': 'san'}]}
print(type(py_obj))  # <class 'dict'>
json_str = json.dumps(py_obj)
# {'first name':True, 'last name':None}=>{"first name": true, "last name": null}
print(json_str, type(json_str))

# python中的元组, 会转换成数组
py_obj1 = {'employees': ('zhangsan', 'lisi', 'Gates')}
json_st1 = json.dumps(py_obj1)
print(json_st1)  # {"employees": ["zhangsan", "lisi", "Gates"]}

print('===================================')
py_obj2 = {'employees': [{'first name': True, 'last name': None}, {'first name': 'Bill', 'last name': 'Gates'},
                         {'first name': 'zhang', 'last name': 'san'}]}
with open('python2json.json', 'w') as f:
    # dump: 将Python对象转换成JSON串, 输出到fp文件流中, fp是一个支持write()的类文件对象
    json.dump(py_obj2, f)
